package cn.usian.controller;

import cn.usian.common.R;
import cn.usian.pojo.Employee;
import cn.usian.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RequestMapping("/employee")
@RestController
public class EmployeeController {
  @Autowired
  private EmployeeService employeeService;
    //登录
    @PostMapping("/login")
    public R login(@RequestBody Employee employee,HttpSession session){
        R r= employeeService.login(employee);
        if (r.getCode()==1){
            //将用户的信息放在session中
            session.setAttribute("employee",r.getData());
        }
        return employeeService.login(employee);
    }

    //注销
    @PostMapping("/logout")
    public R logout(HttpSession session){
        session.invalidate();
        return R.success("退出成功");
    }

  /*  //分页查询
    @GetMapping("page")
   public R findPage(@RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "10")int pageSize,String name){
        Page<Employee> employeePage = new Page<>(pageSize, page);
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        if (name!=null){
            queryWrapper.like(Employee::getName,name);
        }
        Page<Employee> page1 =  employeeService.findPage(employeePage,queryWrapper);
       return R.success(page1);
    }*/

    @GetMapping("page")
    public R  findPage(Integer page,Integer pageSize,String name){
        return employeeService.findPage(page,pageSize,name);
    }

    //用户添加
    @PostMapping
    public R addEmp(@RequestBody  Employee employee,HttpSession session){
        //获取session中的用户信息
     Long id = (Long)session.getAttribute("employee");
     employee.setCreateUser(id);
     employee.setUpdateUser(id);
        return employeeService.add(employee);
  }

  //修改状态
    @PutMapping
    public R updateStatus(@RequestBody Employee employee,HttpSession session){
       Long id = (Long) session.getAttribute("employee");
       employee.setUpdateUser(id);
        return employeeService.updateStatus(employee);
    }

    @GetMapping("{id}")
    public R findById(@PathVariable Long id){
        return employeeService.findById(id);
    }

}


